/*
 *  Copyright 2019-2025 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import me.zhengjie.dao.FreezeTaskRecordDao;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.dao.dto.FreezeTaskRecordQueryCriteria;
import me.zhengjie.mapper.FreezeTaskRecordMapper;
import me.zhengjie.server.FreezeTaskRecordService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author laoxu
 * @description 服务实现
 * @date 2025-07-01
 **/
@Service
@RequiredArgsConstructor
public class FreezeTaskRecordServiceImpl extends ServiceImpl<FreezeTaskRecordMapper, FreezeTaskRecordDao> implements FreezeTaskRecordService {

    private final FreezeTaskRecordMapper freezeTaskRecordMapper;

    @Override
    public PageResult<FreezeTaskRecordDao> queryAll(FreezeTaskRecordQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(freezeTaskRecordMapper.findAll(criteria, page));
    }

    @Override
    public List<FreezeTaskRecordDao> queryAll(FreezeTaskRecordQueryCriteria criteria) {
        return freezeTaskRecordMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(FreezeTaskRecordDao resources) {
        freezeTaskRecordMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(FreezeTaskRecordDao resources) {
        FreezeTaskRecordDao freezeTaskRecordDao = getById(resources.getId());
        freezeTaskRecordDao.copy(resources);
        freezeTaskRecordMapper.updateById(freezeTaskRecordDao);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<String> ids) {
        freezeTaskRecordMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<FreezeTaskRecordDao> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (FreezeTaskRecordDao freezeTaskRecordDao : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("任务ID", freezeTaskRecordDao.getTaskId());
            map.put("楼层", freezeTaskRecordDao.getFloor());
            map.put("冻结时间", freezeTaskRecordDao.getFreezeTime());
            map.put("解冻时间", freezeTaskRecordDao.getUnfreezeTime());
            map.put("冻结账号", freezeTaskRecordDao.getFreezeName());
            map.put("解冻账号", freezeTaskRecordDao.getUnfreezeName());
            map.put("冻结历史", freezeTaskRecordDao.getFreezeHistory());
            map.put("冻结描述", freezeTaskRecordDao.getFreezeDes());
            map.put("冻结信息", freezeTaskRecordDao.getFreezeNotes());
            map.put("物料的唯一标识", freezeTaskRecordDao.getItemBarCode());
            map.put("物料的编号", freezeTaskRecordDao.getMaterialCode());
            map.put("物料的名称", freezeTaskRecordDao.getMaterialName());
            map.put("物料的数量", freezeTaskRecordDao.getInventoryQuantity());
            map.put("物料的计量单位", freezeTaskRecordDao.getUnit());
            map.put("物料的规格描述", freezeTaskRecordDao.getSpecs());
            map.put("物料的批次号", freezeTaskRecordDao.getBatchCode());
            map.put("冻结（0冻结/1启用）", freezeTaskRecordDao.getDisable());
            map.put("原物料的生产厂家", freezeTaskRecordDao.getManufacturers());
            map.put("用于标识载具的唯一编码", freezeTaskRecordDao.getContainerCode());
            map.put("原料、辅料、半成品", freezeTaskRecordDao.getMaterialCategory());
            map.put("3F 1.在制品 2.实体物料", freezeTaskRecordDao.getMaterial());
            map.put("入厂时间", freezeTaskRecordDao.getEntryTime());
            map.put("半成品", freezeTaskRecordDao.getSupplierCustomer());
            map.put("仓库", freezeTaskRecordDao.getWarehouse());
            map.put("发出仓位", freezeTaskRecordDao.getIssuePosition());
            map.put("操作人", freezeTaskRecordDao.getOperator());
            map.put("操作时间", freezeTaskRecordDao.getOperationTime());
            map.put("Color", freezeTaskRecordDao.getColor());
            map.put("MeasurementMethod", freezeTaskRecordDao.getMeasurementMethod());
            map.put("MeedleCount", freezeTaskRecordDao.getMeedleCount());
            map.put("Size", freezeTaskRecordDao.getSize());
            map.put("SizePage", freezeTaskRecordDao.getSizePage());
            map.put("WorkingNo", freezeTaskRecordDao.getWorkingNo());
            map.put("产品类型", freezeTaskRecordDao.getProductType());
            map.put("包装方式", freezeTaskRecordDao.getPackagingMethod());
            map.put("特殊工艺", freezeTaskRecordDao.getSpecialProcess());
            map.put("程式名", freezeTaskRecordDao.getProgramName());
            map.put("编织机器", freezeTaskRecordDao.getWeavingMachine());
            map.put("HCBanderole", freezeTaskRecordDao.getHcbanderole());
            map.put("composition", freezeTaskRecordDao.getComposition());
            map.put("Qclab", freezeTaskRecordDao.getQclab());
            map.put("包装配比", freezeTaskRecordDao.getPackagingRatio());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatchData(List<FreezeTaskRecordDao> list) {
        this.saveBatch(list);
    }

    @Override
    public List<FreezeTaskRecordDao> findRecordByItemBarCodeList(List<String> itemBarCodeList) {
        LambdaQueryWrapper<FreezeTaskRecordDao> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(FreezeTaskRecordDao::getItemBarCode, itemBarCodeList);
        wrapper.eq(FreezeTaskRecordDao::getDisable, 1);
        wrapper.eq(FreezeTaskRecordDao::getFreezeHistory, 0);
        return freezeTaskRecordMapper.selectList(wrapper);
    }

    @Override
    public void updateRecordByItemBarCodeList(List<String> itemBarCodeList, String handleName) {
        LambdaUpdateWrapper<FreezeTaskRecordDao> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(FreezeTaskRecordDao::getItemBarCode, itemBarCodeList);
        wrapper.eq(FreezeTaskRecordDao::getDisable, 1);
        wrapper.eq(FreezeTaskRecordDao::getFreezeHistory, 0);
        wrapper.set(FreezeTaskRecordDao::getFreezeHistory, 1);
        wrapper.set(FreezeTaskRecordDao::getDisable, 0);
        wrapper.set(FreezeTaskRecordDao::getUnfreezeName, handleName);
        wrapper.set(FreezeTaskRecordDao::getUnfreezeTime, LocalDateTime.now());
    }
}